]> git.r.bdr.sh - rbdr/super-polarity/blob - Super Polarity/Actors/MainShip.cs
I think bullets come out now.
[rbdr/super-polarity] / Super Polarity / Actors / MainShip.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading;
6 using Microsoft.Xna.Framework;
7 using Microsoft.Xna.Framework.Content;
8 using Microsoft.Xna.Framework.Graphics;
9
10 namespace SuperPolarity
11 {
12 class MainShip : Ship
13 {
14
15 public static Color BlueColor;
16 public static Color RedColor;
17
18 ParticleEngine particleEngine;
19
20 protected bool Shooting;
21 protected int ShotCooldown;
22
23 public MainShip(Game newGame) : base(newGame) {}
24
25 ~MainShip()
26 {
27 particleEngine = null;
28 }
29
30 public override void Initialize(Texture2D texture, Vector2 position)
31 {
32 base.Initialize(texture, position);
33
34 MainShip.BlueColor = new Color(230, 244, 249);
35 MainShip.RedColor = new Color(255, 234, 241);
36
37 InitParticleEngine();
38 SetPolarity(Polarity.Positive);
39
40 ShotCooldown = 50;
41
42 BindInput();
43 }
44
45 void InitParticleEngine()
46 {
47 particleEngine = ParticleEffectFactory.CreatePolarCircle(Position);
48 }
49
50 void BindInput()
51 {
52 InputController.Bind("moveX", HandleHorizontalMovement);
53 InputController.Bind("moveY", HandleVerticalMovement);
54 InputController.Bind("changePolarity", HandleChangePolarity);
55 InputController.Bind("shoot", HandleShot);
56 }
57
58 protected void HandleShot(float value)
59 {
60 Children.Add(ActorFactory.CreateBullet(Position, Angle));
61 Shooting = true;
62 Timer t = new Timer(new TimerCallback(UnlockShot));
63 t.Change(ShotCooldown, Timeout.Infinite);
64 }
65
66 protected void UnlockShot(object state)
67 {
68 InputController.Unlock("shoot");
69 }
70
71 protected void HandleChangePolarity(float value)
72 {
73 SwitchPolarity();
74 }
75
76 public void HandleHorizontalMovement(float value)
77 {
78 Acceleration.X = value * AccelerationRate;
79
80 if (value > 0.1 && Velocity.X < 0 || value < 0.1 && Velocity.X > 0)
81 {
82 Acceleration.X *= 2;
83 }
84
85 if (value > 0.1 && Velocity.Y < 0 || value < 0.1 && Velocity.Y > 0)
86 {
87 Acceleration.Y *= 2;
88 }
89 }
90
91 public void HandleVerticalMovement(float value)
92 {
93 Acceleration.Y = value * AccelerationRate;
94 }
95
96 public override void SwitchPolarity()
97 {
98 base.SwitchPolarity();
99 SwitchParticleEngine(CurrentPolarity);
100 }
101
102 public override void SetPolarity(Polarity newPolarity)
103 {
104 base.SetPolarity(newPolarity);
105 SwitchParticleEngine(newPolarity);
106 }
107
108 protected void SwitchParticleEngine(Polarity polarity)
109 {
110 if (polarity == Polarity.Positive)
111 {
112 particleEngine.Color = MainShip.RedColor;
113 }
114 else if (polarity == Polarity.Negative)
115 {
116 particleEngine.Color = MainShip.BlueColor;
117 }
118 else
119 {
120 particleEngine.Color = Color.Gray;
121 }
122 }
123
124 public override void Update(GameTime gameTime)
125 {
126 base.Update(gameTime);
127 particleEngine.EmitterLocation = Position;
128 particleEngine.Update();
129 ConstrainToEdges();
130 Shooting = false;
131 }
132
133 public override void Move(GameTime gameTime)
134 {
135 base.Move(gameTime);
136
137 if (Shooting)
138 {
139 if (Velocity.X > ActVelocity)
140 {
141 Velocity.X = ActVelocity;
142 }
143
144 if (Velocity.X < -ActVelocity)
145 {
146 Velocity.X = -ActVelocity;
147 }
148
149 if (Velocity.Y > ActVelocity)
150 {
151 Velocity.Y = ActVelocity;
152 }
153
154 if (Velocity.Y < -ActVelocity)
155 {
156 Velocity.Y = -ActVelocity;
157 }
158 }
159 }
160
161 public override void Magnetize(Ship ship, float distance, float angle)
162 {
163 }
164
165 protected void ConstrainToEdges()
166 {
167 if (Position.X < 0)
168 {
169 Position.X = 0;
170
171 if (Velocity.X < 0)
172 {
173 Velocity.X = 0;
174 }
175 }
176 if (Position.X > game.GraphicsDevice.Viewport.Width)
177 {
178 Position.X = game.GraphicsDevice.Viewport.Width;
179
180 if (Velocity.X > 0)
181 {
182 Velocity.X = 0;
183 }
184 }
185 if (Position.Y < 0)
186 {
187 Position.Y = 0;
188
189 if (Velocity.Y < 0)
190 {
191 Velocity.Y = 0;
192 }
193 }
194 if (Position.Y > game.GraphicsDevice.Viewport.Height)
195 {
196 Position.Y = game.GraphicsDevice.Viewport.Height;
197
198 if (Velocity.Y < 0)
199 {
200 Velocity.Y = 0;
201 }
202 }
203 }
204
205 public override void Draw(SpriteBatch spriteBatch)
206 {
207 particleEngine.Draw(spriteBatch);
208 base.Draw(spriteBatch);
209 }
210 }
211 }